package service.impl;

import dao.MenuDao;
import dao.impl.MenuDaoImpl;
import model.Menu;
import service.MenuService;

import java.util.ArrayList;
import java.util.List;

public class MenuServiceImpl implements MenuService {
    MenuDao menuDao = new MenuDaoImpl();

    @Override
    public List<Menu> getMenuList() {
        List<Menu> menuList = menuDao.getMenuList();//未分级
        List<Menu> newMenuList = new ArrayList<>();//保存分级以后的菜单
        for (Menu menu : menuList) {
            if (menu.getUpmenuId() == 0) {
                //说明是一级菜单
                List<Menu> secondList = new ArrayList<>();
                for (Menu second : menuList) {
                    if (second.getUpmenuId() == menu.getMenuId()) {
                        secondList.add(second);
                    }
                }
                menu.setSecondMenus(secondList);
                newMenuList.add(menu);
                //这时候已经分完级别
            }
        }
        return newMenuList;
    }
}
